<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Univariable</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <%
        #set task id
        if(is.null(COOKIES$"task")){
        taskid <- format(Sys.time(), "%y%m%d%H%M%S")
        filepath <- file.path("/var/www/Rcloud/upload", taskid)
        dir.create(filepath)
	setwd(filepath)
        setCookie("task", taskid)
        #save(taskid, file=paste(filepath, "/", taskid, ".rda", sep=""))
        cat(paste("A new task created. (id:", taskid, ")"))
        }else{
        taskid <- unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[1]
        #taskid <- COOKIES$"task"
        filepath <- file.path("/var/www/Rcloud/upload", taskid)
	dir.create(filepath)
        setwd(filepath)
        p<-paste("Task id: ", taskid, "<br>")
        cat(p)
        }
        %>
        <h3>Visualization for Univariable Data</h3>
        <form name="data" action="#" method="GET">
            Select your data:
            <select name="dataname">
                <% #load listfile
                if(file.exists("listfile.rda")){
                load("listfile.rda")
                for(n in 1:length(filelist)){
                cat("<option value=", n, ">", filelist[[n]]$txt, "</option>")
                }
                }
                %>
            </select>
            <input type="submit" value="Submit" />
        </form>
        <%
        if(!is.null(GET$dataname)){
        N <- GET$dataname
        task <- paste(taskid, N, sep="|") #add data num to task
        setCookie("task", task)
        N <- as.numeric(N)
        cat("Data <I>", filelist[[N]]$txt, "</I> was selected.<br>")
        #idata <- read.table(filelist[[n]]$txt, header=as.logical(filelist[[n]]$head), sep=filelist[[n]]$sep)
        }
        %>
        <%
        if(!is.na(unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[2])){
        N <- unlist(strsplit(COOKIES$"task", "|", fixed=TRUE))[2]
        N <- as.numeric(N)
        cat("Data <I>", filelist[[N]]$txt, "</I> was selected.<br>")
        idata <- read.table(filelist[[N]]$txt, header=as.logical(filelist[[n]]$head), sep=filelist[[n]]$sep)
        if(filelist[[N]]$rownames=="first"){
        rownames(idata) <- as.character(idata[,1])
        idata <- as.data.frame(idata[, -1])
        }
	}
        %>
        <p>
        <form name="summary" action="#" method="GET">
            Data summary:
            <input type="submit" value="Run" name="summary" />
        </form>
        <%
        if(!is.null(GET$"summary") && exists("idata")){
        dsm <- summary(idata)
        cat("<table style='border:1px;border-style:dashed;border-width:1px 1px 1px 1px;'>")
            cat("<tbody>")
                for(i in 1:6){
                cat("<tr>")
                    cat("<td>", dsm[i], "</td>")
                cat("</tr>")
                }
            cat("</tbody>")
        cat("</table>")
        }
        %>
        <form name="hist" action="#" method="GET">
            Histograms:
            lines<input type="checkbox" name="lines" value="ON" checked="checked" />
            rug<input type="checkbox" name="rug" value="ON" checked="checked" />
            skewness<input type="checkbox" name="skewness" value="ON"  />
            kurtosis<input type="checkbox" name="kurtosis" value="ON"  />
            <input type="submit" value="Plot" name="hist" />
        </form>
        <%
        if(!is.null(GET$hist)){
        png(paste("/var/www/images/R/", taskid, "hist.png", sep=""))
        data <- idata[,1]
        hist(data, probability=TRUE, main=paste("Histogram of", colnames(idata)), xlab=colnames(idata))
        if(!is.null(GET$lines)){
        lines(density(data))
        }
        if(!is.null(GET$rug)){
        rug(data)
        }
        if(!is.null(GET$skewness) || !is.null(GET$kurtosis)){
        library("class")
        library("e1071")
        if(!is.null(GET$skewness)){
        mtext(paste("skewness =", round(skewness(data), digits=4)), adj=1)
        }
        if(!is.null(GET$kurtosis)){
        mtext(paste("kurtosis =", round(kurtosis(data), digits=4)), adj=NA)
        }
        }
        dev.off()
        #png url
        purl <- paste("../images/R/", taskid, "hist.png", sep="")
        cat("<img src='", purl, "'/>", sep="")
        }
        %>
        <form name="qq" action="#">
        Normal Q-Q plot:
        <input type="submit" value="Plot" name="qq" />
        </form>
        <%
        if(!is.null(GET$qq)){
        png(paste("/var/www/images/R/", taskid, "qq.png", sep=""))
        data <- idata[,1]
        qqnorm(data)
        qqline(data, col="red")
        dev.off()
        purl <- paste("../images/R/", taskid, "qq.png", sep="")
        cat("<img src='", purl, "'/>", sep="")
        }
        %>

    </body>
</html>
